package com.group.Controller;

import cn.hutool.json.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.group.Enity.CarCharge;
import com.group.Enity.Repair;
import com.group.Enity.RepairType;
import com.group.Service.RepairService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

@Controller
@RequestMapping("repair")
public class RepairController {

    @Autowired
    private RepairService repairService;

    @ResponseBody
    @RequestMapping("queryRepairAll")
    public String queryRepairAll(Integer page,Integer limit,Integer status,Integer comId){

        PageHelper.startPage(page,limit);
        List<Repair> list = repairService.getRepairs(status,comId);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("queryRepairAll2")
    public String queryRepairAll2(Integer page,Integer limit,Integer status,Integer comId){

        PageHelper.startPage(page,limit);
        List<Repair> list = repairService.getRepairs(status,comId);

        PageInfo pageInfo = new PageInfo<>(list);
        Long len = pageInfo.getTotal();
        List<CarCharge> list_new = pageInfo.getList();

        JSONObject json=new JSONObject();
        json.accumulate("count",len);
        json.accumulate("code",0);
        json.accumulate("msg","null");
        json.accumulate("data",list_new);
        return json.toString();
    }

    @ResponseBody
    @RequestMapping("queryAll")
    public List queryAll(){
        List<RepairType> list = repairService.getRepairTypes();
        return list;
    }

    @ResponseBody
    @RequestMapping("queryTongJi")
    public List<HashMap<String,String>> queryTongJi()
    {

        List<HashMap<String,String>> list = new ArrayList<>();
        List<HashMap<String,Integer>> countsMap = repairService.getCounts();

        Iterator iterator = countsMap.iterator();
        while (iterator.hasNext())
        {
            HashMap<String,Integer> integerHashMap = (HashMap<String, Integer>) iterator.next();

            HashMap<String,String> hashMap = new HashMap<>();
            hashMap.put("name", String.valueOf(integerHashMap.get("name")));
            hashMap.put("counts", String.valueOf(integerHashMap.get("counts")) );
            list.add(hashMap);
        }

        return list;
    }

    @ResponseBody
    @RequestMapping("deleteByIds")
    public JSONObject deleteByIds(String ids){

        String[] list = ids.split(",");
        System.out.println(ids);
        for (int step = 0;step<list.length;step++)
        {
            int id = Integer.parseInt(list[step]);
            repairService.deleteById(id);
        }
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

    @ResponseBody
    @RequestMapping("update")
    public JSONObject update(String id){

        //System.out.println(id);
        repairService.update(Integer.parseInt(id));
        JSONObject json=new JSONObject();
        json.accumulate("code",200);
        return json;
    }

}
